<template>
    <div class="header">
        <div class="nav-topbar">
            <div class="container">
                <div class="topbar-menu">
                    <a  href="javascript:;">
                        <h2 style="font-family: 'Avenir', Helvetica, Arial, sans-serif">
                            国粹文化 | 艺术瑰宝
                        </h2>
                    </a>
                </div>
                <div class="topbar-user">
                    <el-dropdown :hide-on-click="false">
                        <span class="el-dropdown-link">
                      <a href="javascript:;" v-if="username">{{username}}</a>
                    </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item>
                                <a href="javascript:;" @click="goToInfo" style="color: #757575">
                                    个人中心
                                </a>
                            </el-dropdown-item>
                            <el-dropdown-item>
                                <a href="javascript:;" @click="goToCart" style="color: #757575">
                                    购物车({{cartCount}})
                                </a>
                            </el-dropdown-item>
                            <el-dropdown-item>
                                <a href="javascript:;" @click="goToCollection" style="color: #757575">
                                    我的收藏({{cartCount}})
                                </a>
                            </el-dropdown-item>
                            <el-dropdown-item disabled>开发中</el-dropdown-item>

                        </el-dropdown-menu>
                    </el-dropdown>
                    <a href="javascript:;" v-if="!username" @click="login">登录</a>
                    <a href="javascript:;" v-if="username" @click="logout">退出</a>
                    <a href="/#/order/list" v-if="username">我的订单</a>
                    <a href="javascript:;" @click="getMessage">
                        <el-popover placement="bottom" width="350" trigger="hover">
                            <!--通知内容-->
                            <div>
                                <span v-if="!msg" style="display: contents">暂没有新消息</span>
                                <!--消息列表-->
                                <div style="margin-top: 0;width: 100%" v-if="msg">
                                    消息列表
                                </div>
                            </div>
                            <el-button slot="reference" style="border-radius: 0px" type="warning">
                                <span class="el-icon-bell"></span>
                                通知({{cartCount}})
                            </el-button>
                        </el-popover>
                    </a>
                    <a href="javascript:;" @click="goToCart" style="margin-left: -17px;">
                        <el-button style="border-radius: 0px;" type="success">
                            <span class="icon-cart"></span>
                            购物车({{cartCount}})
                        </el-button>
                    </a>
                </div>
            </div>
        </div>
        <div class="nav-header">
            <div class="container">
                <div class="header-logo">
                    <a href="/#/index"></a>
                </div>
                <!--首页头部导航-->
                <div class="header-menu">
                    <div class="item-menu">
                        <span>人气推荐</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in productList" :key="index">
                                    <a v-bind:href="'/#/detail/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.pic" :alt="item.subTitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.originalPrice | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu">
                        <span>新品推荐</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in newProductList" :key="index">
                                    <a v-bind:href="'/#/detail/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.pic" :alt="item.subTitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.originalPrice | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu">
                        <span>非遗</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in productList" :key="index">
                                    <a v-bind:href="'/#/detail/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.pic" :alt="item.subTitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.originalPrice | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu">
                        <span>大吴泥塑</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in productList" :key="index">
                                    <a v-bind:href="'/#/detail/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.pic" :alt="item.subTitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.originalPrice | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu">
                        <span>金漆木雕</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in productList" :key="index">
                                    <a v-bind:href="'/#/detail/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.pic" :alt="item.subTitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.originalPrice | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu">
                        <span>麦秆画</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in productList" :key="index">
                                    <a v-bind:href="'/#/detail/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.pic" :alt="item.subTitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.originalPrice | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
                <!--搜索-->
                <div class="header-search">
                    <div class="wrapper">
                        <input type="text" v-model="keyword" name="keyword">
                        <!--@keyup="doSearch()"-->
                        <a href="javascript:;" @click="doSearch()"></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {mapState} from 'vuex'
    import GoodsSale from './index/GoodsSale'

    export default {
        name: 'nav-header',
        props: ['keywords'],
        data() {
            return {
                msg: null,
                productList: [],
                newProductList: [],
                keyword: "请输入产品关键字"
            }
        },
        computed: {
            username() {
                return this.$store.state.username;
            },
            cartCount() {
                // 取
                return this.$store.state.cartCount;
            },
            ...mapState(['username', 'cartCount'])
        },
        filters: {
            currency(val) {
                if (!val) return '0.00';
                return '￥' + val.toFixed(2) + '元';
            }
        },
        mounted() {
            this.getHotProductList();
            let params = this.$route.params;
            if (params && params.from == 'login') {
                this.getCartCount();
            }
        },
        methods: {
            login() {
                this.$router.push('/login');
            },
            getHotProductList() {
                //获取人气推荐品信息
                this.axios.get('/home/hotProduct').then((response) => {
                    this.productList = response;
                })

                //获取新品推荐
                this.axios.get('/home/newProduct').then((response) => {
                    this.newProductList = response;
                })
            },

            //获取购物车商品数量
            getCartCount() {
            },

            logout() {
                this.$cookie.set('token', '');
                this.$cookie.set('username', '');
                this.$message.success('退出成功');
                this.$store.dispatch('saveUserName', '');
                this.$store.dispatch('saveCartCount', '0');

            },
            doSearch() {
                //携带搜索关键字跳转到searchResult页面，在searchResutl页面完成搜索并显示
                this.$router.push(`/searchResult/${this.keyword}`).catch(err => {
                    window.console.log(err)
                });
            },
            goToCart() {
                this.$router.push('/cart');
            },
            //获取通知消息
            getMessage() {

            },
            //我的收藏
            goToCollection() {
                alert("程序员小哥哥正在开发中。。。")
            },

            //前往个人中心
            goToInfo() {
                this.$router.push('/personalCenter');
            }
        }
    }
</script>
<style lang="scss">
    @import './../assets/scss/base.scss';
    @import './../assets/scss/mixin.scss';
    @import './../assets/scss/config.scss';

    .el-dropdown-link {
        cursor: pointer;
        color: #80c58a;
    }

    .el-icon-arrow-down {
        font-size: 10px;
    }

    .header {
        .nav-topbar {
            height: 40px;
            line-height: 39px;
            background-color: #333333;
            color: #B0B0B0;

            .container {
                @include flex();

                a {
                    display: inline-block;
                    color: #B0B0B0;
                    margin-right: 17px;
                }

                .my-cart {
                    height: 40px;
                    width: 110px;
                    background-color: #7ECF68;
                    text-align: center;
                    color: #ffffff;
                    margin-right: 0;

                    .icon-cart {
                        @include bgImg(16px, 12px, '/imgs/icon-cart-checked.png');
                        margin-right: 4px;
                    }
                }

                /*.my-msg{
                  height: 40px;
                  width:110px;
                  background-color:#f25807;
                  text-align:center;
                  color:#ffffff;
                  margin-right:0;
                }*/
            }
        }

        .nav-header {
            .container {
                position: relative;
                height: 112px;
                @include flex();

                .header-menu {
                    display: inline-block;
                    width: 643px;
                    padding-left: 209px;

                    .item-menu {
                        display: inline-block;
                        color: #333333;
                        font-weight: bold;
                        font-size: 16px;
                        line-height: 112px;
                        margin-right: 20px;

                        span {
                            cursor: pointer;
                        }

                        &:hover {
                            color: #7ECF68;

                            .children {
                                height: 220px;
                                opacity: 1;
                                //弹出后不被原有的div覆盖
                                z-index: 100;
                            }
                        }

                        .children {
                            position: absolute;
                            top: 112px;
                            left: 0;
                            width: 1226px;
                            height: 0;
                            opacity: 0;
                            overflow: hidden;
                            border-top: 1px solid #E5E5E5;
                            box-shadow: 0px 7px 6px 0px rgba(0, 0, 0, 0.11);
                            z-index: 10;
                            transition: all .5s;
                            background-color: #ffffff;

                            .product {
                                position: relative;
                                float: left;
                                width: 16.6%;
                                height: 220px;
                                font-size: 12px;
                                line-height: 12px;
                                text-align: center;

                                a {
                                    display: inline-block;
                                }

                                img {
                                    width: auto;
                                    height: 111px;
                                    margin-top: 26px;
                                }

                                .pro-img {
                                    height: 137px;
                                }

                                .pro-name {
                                    font-weight: bold;
                                    margin-top: 19px;
                                    margin-bottom: 8px;
                                    color: $colorB;
                                }

                                .pro-price {
                                    color: #7ECF68;
                                }

                                &:before {
                                    content: ' ';
                                    position: absolute;
                                    top: 28px;
                                    right: 0;
                                    border-left: 1px solid $colorF;
                                    height: 100px;
                                    width: 1px;
                                }

                                &:last-child:before {
                                    display: none;
                                }
                            }
                        }
                    }
                }

                .header-search {
                    width: 319px;

                    .wrapper {
                        height: 50px;
                        border: 1px solid #E0E0E0;
                        display: flex;
                        align-items: center;

                        input {
                            border: none;
                            box-sizing: border-box;
                            border-right: 1px solid #E0E0E0;
                            width: 264px;
                            height: 50px;
                            padding-left: 14px;
                        }

                        a {
                            @include bgImg(18px, 18px, '/imgs/icon-search.png');
                            margin-left: 17px;
                        }
                    }
                }
            }
        }
    }
</style>